using AntdUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TangCaptureTraffic.Forms
{
    public partial class SplashForm2 : AntdUI.Window
    {

        /// <summary>
        /// 当前加载状态
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 当前加载状态
        /// </summary>
        public string Status
        {
            get => statusLabel.Text;
            set => statusLabel.Text = value;
        }

        /// <summary>
        /// 当前进度值(0-100)
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 当前进度值(0-100)
        /// </summary>
        public float ProgressValue
        {
            get => progress.Value;
            set => progress.Value = value;
        }
        public SplashForm2()
        {
            InitializeComponent();
            // 确保进度条初始值为0
            this.progress.Value = 0;
            // 初始状态不显示文本，等待UpdateStatus方法被调用
            this.statusLabel.Text = "";
        }

        private void SplashForm2_Load(object sender, EventArgs e)
        {
            versionLabel.Text = $"Version {Application.ProductVersion}";
        }
        /// <summary>
        /// 更新加载状态和进度
        /// </summary>
        /// <param name="status">状态描述</param>
        /// <param name="progressValue">进度值(0-100)</param>
        public async Task UpdateStatus(string status, float progressValue)
        {
            if (progressValue > 1) progressValue = progressValue / 100;
            if (this.InvokeRequired)
            {
                await this.Invoke(async () => await UpdateStatus(status, progressValue));
                return;
            }

            // 先更新状态文本，确保文字状态立即显示
            this.Status = status;
            Application.DoEvents(); // 确保UI立即更新状态文本

            // 平滑更新进度条
            float currentValue = this.ProgressValue;
            float targetValue = progressValue;

            // 如果目标值小于当前值，直接设置（避免进度倒退）
            if (targetValue < currentValue)
            {
                this.ProgressValue = targetValue;
                Application.DoEvents(); // 确保UI更新
                return;
            }

            // 如果差值很小，直接设置
            if (targetValue - currentValue < 3)
            {
                this.ProgressValue = targetValue;
                Application.DoEvents(); // 确保UI更新
                return;
            }

            // 平滑过渡到目标值
            const int steps = 15; // 增加平滑步数
            float stepValue = (targetValue - currentValue) / steps;

            // 平滑更新进度条
            for (int i = 0; i < steps; i++)
            {
                currentValue += stepValue;
                this.ProgressValue = currentValue;
                Application.DoEvents(); // 确保UI更新
                await Task.Delay(15); // 增加延迟时间以使平滑效果更明显
            }

            // 确保最终值精确
            this.ProgressValue = targetValue;
            Application.DoEvents(); // 确保UI更新
        }

        /// <summary>
        /// 设置完成状态并关闭窗体
        /// </summary>
        public async Task SetCompleteAndClose()
        {
            // 完成进度
            await UpdateStatus("加载完成", 100);
            await Task.Delay(500); // 短暂延迟以显示完成状态

            // 设置对话框结果为OK并关闭窗体
            this.DialogResult = DialogResult.OK;
        }
    }
}
